﻿using System;
using WWDemon.Core.MessageHub;
using WWDemon.Model;

namespace WWDemon.Core
{
    public class GlobalSubPub
    {
        private static readonly object objLock = new object();
        private static GlobalSubPub  globalSubPub;

        public static GlobalSubPub Instance
        {
            get
            {
                if (globalSubPub == null)
                    lock (objLock)
                    {
                        if (globalSubPub == null) globalSubPub = new GlobalSubPub();
                    }

                return globalSubPub;
            }
        }


        private IMessageHub hub;

        public GlobalSubPub()
        {
            hub = new MessagePub();
        }

        public void ClearSubscriptions()
        {
            hub.ClearSubscriptions();
        }

        public bool IsSubscribed(Guid token)
        {
            return hub.IsSubscribed(token);
        }

        public void Publish<T>(CommondKey k, T message) 
        {
            hub.Publish<T>(k, message);
        }
        public void PublishAsync<T>(CommondKey k, T message) 
        {
            hub.PublishAsync<T>(k, message);
        }

        public void RegisterGlobalErrorHandler(Action<Guid, Exception> onError)
        {
            hub.RegisterGlobalErrorHandler(onError);
        }

        public void RegisterGlobalHandler(Action<Type, object> onMessage)
        {
            hub.RegisterGlobalHandler(onMessage);
        }

        public Guid Subscribe<T>(CommondKey k, Action<T> action) 
        {
            return hub.Subscribe<T>(k, action);
        }

        public Guid Subscribe<T>(CommondKey k, Action<T> action, TimeSpan throttleBy) 
        {
            return hub.Subscribe<T>(k, action, throttleBy);
        }

        public void Unsubscribe(Guid token)
        {
            hub.Unsubscribe(token);
        }
    }


}
